home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / Perl5001 / Manual / perlguts_h < prev    next >
Text File  |  1995-04-18  |  19KB  |  466 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLGUTS </TITLE>
  5. <h2>NAME</h2>
  6. perlguts - Perl's Internal Functions
  7. <p><h2>DESCRIPTION</h2>
  8. This document attempts to describe some of the internal functions of the
  9. Perl executable.  It is far from complete and probably contains many errors.
  10. Please refer any questions or comments to the author below.
  11. <p><h2>Datatypes</h2>
  12. Perl has three typedefs that handle Perl's three main data types:
  13. <p><pre>
  14.         SV  Scalar Value
  15.         AV  Array Value
  16.         HV  Hash Value
  17. </pre>
  18. Each typedef has specific routines that manipulate the various data type.
  19. <p><h3>What is an "IV"?</h3>
  20. Perl uses a special typedef IV which is large enough to hold either an
  21. integer or a pointer.
  22. <p>Perl also uses a special typedef I32 which will always be a 32-bit integer.
  23. <p><h3>Working with SV's</h3>
  24. An SV can be created and loaded with one command.  There are four types of
  25. values that can be loaded: an integer value (IV), a double (NV), a string,
  26. (PV), and another scalar (SV).
  27. <p>The four routines are:
  28. <p><pre>
  29.         SV*  newSViv(IV);
  30.         SV*  newSVnv(double);
  31.         SV*  newSVpv(char*, int);
  32.         SV*  newSVsv(SV*);
  33. </pre>
  34. To change the value of an *already-existing* scalar, there are five routines:
  35. <p><pre>
  36.         void  sv_setiv(SV*, IV);
  37.         void  sv_setnv(SV*, double);
  38.         void  sv_setpvn(SV*, char*, int)
  39.         void  sv_setpv(SV*, char*);
  40.         void  sv_setsv(SV*, SV*);
  41. </pre>
  42. Notice that you can choose to specify the length of the string to be
  43. assigned by using <B>sv_setpvn</B>, or allow Perl to calculate the length by
  44. using <B>sv_setpv</B>.  Be warned, though, that <B>sv_setpv</B> determines the
  45. string's length by using <B>strlen</B>, which depends on the string terminating
  46. with a NUL character.
  47. <p>To access the actual value that an SV points to, you can use the macros:
  48. <p><pre>
  49.         SvIV(SV*)
  50.         SvNV(SV*)
  51.         SvPV(SV*, STRLEN len)
  52. </pre>
  53. which will automatically coerce the actual scalar type into an IV, double,
  54. or string.
  55. <p>In the <B>SvPV</B> macro, the length of the string returned is placed into the
  56. variable <B>len</B> (this is a macro, so you do <I>not</I> use <B>&len</B>).  If you do not
  57. care what the length of the data is, use the global variable <B>na</B>.  Remember,
  58. however, that Perl allows arbitrary strings of data that may both contain
  59. NUL's and not be terminated by a NUL.
  60. <p>If you simply want to know if the scalar value is TRUE, you can use:
  61. <p><pre>
  62.         SvTRUE(SV*)
  63. </pre>
  64. Although Perl will automatically grow strings for you, if you need to force
  65. Perl to allocate more memory for your SV, you can use the macro
  66. <p><pre>
  67.         SvGROW(SV*, STRLEN newlen)
  68. </pre>
  69. which will determine if more memory needs to be allocated.  If so, it will
  70. call the function <B>sv_grow</B>.  Note that <B>SvGROW</B> can only increase, not
  71. decrease, the allocated memory of an SV.
  72. <p>If you have an SV and want to know what kind of data Perl thinks is stored
  73. in it, you can use the following macros to check the type of SV you have.
  74. <p><pre>
  75.         SvIOK(SV*)
  76.         SvNOK(SV*)
  77.         SvPOK(SV*)
  78. </pre>
  79. You can get and set the current length of the string stored in an SV with
  80. the following macros:
  81. <p><pre>
  82.         SvCUR(SV*)
  83.         SvCUR_set(SV*, I32 val)
  84. </pre>
  85. But note that these are valid only if <B>SvPOK()</B> is true.
  86. <p>If you know the name of a scalar variable, you can get a pointer to its SV
  87. by using the following:
  88. <p><pre>
  89.         SV*  perl_get_sv("varname", FALSE);
  90. </pre>
  91. This returns NULL if the variable does not exist.
  92. <p>If you want to know if this variable (or any other SV) is actually defined,
  93. you can call:
  94. <p><pre>
  95.         SvOK(SV*)
  96. </pre>
  97. The scalar 
  98. <A HREF="perlfunc.html#perlfunc_258">undef</A>
  99.  value is stored in an SV instance called <B>sv_undef</B>.  Its
  100. address can be used whenever an <B>SV*</B> is needed.
  101. <p>There are also the two values <B>sv_yes</B> and <B>sv_no</B>, which contain Boolean
  102. TRUE and FALSE values, respectively.  Like <B>sv_undef</B>, their addresses can
  103. be used whenever an <B>SV*</B> is needed.
  104. <p>Do not be fooled into thinking that <B>(SV *) 0</B> is the same as <B>&sv_undef</B>.
  105. Take this code:
  106. <p><pre>
  107.         SV* sv = (SV*) 0;
  108.         if (I-am-to-return-a-real-value) {
  109.                 sv = sv_2mortal(newSViv(42));
  110.         }
  111.         sv_setsv(ST(0), sv);
  112. </pre>
  113. This code tries to return a new SV (which contains the value 42) if it should
  114. return a real value, or undef otherwise.  Instead it has returned a null
  115. pointer which, somewhere down the line, will cause a segmentation violation,
  116. or just weird results.  Change the zero to <B>&sv_undef</B> in the first line and
  117. all will be well.
  118. <p>To free an SV that you've created, call <B>SvREFCNT_dec(SV*)</B>.  Normally this
  119. call is not necessary.  See the section on <B>MORTALITY</B>.
  120. <p><h3>Private and Public Values</h3>
  121. Recall that the usual method of determining the type of scalar you have is
  122. to use <B>Sv[INP]OK</B> macros.  Since a scalar can be both a number and a string,
  123. usually these macros will always return TRUE and calling the <B>Sv[INP]V</B>
  124. macros will do the appropriate conversion of string to integer/double or
  125. integer/double to string.
  126. <p>If you <I>really</I> need to know if you have an integer, double, or string
  127. pointer in an SV, you can use the following three macros instead:
  128. <p><pre>
  129.         SvIOKp(SV*)
  130.         SvNOKp(SV*)
  131.         SvPOKp(SV*)
  132. </pre>
  133. These will tell you if you truly have an integer, double, or string pointer
  134. stored in your SV.
  135. <p>In general, though, it's best to just use the <B>Sv[INP]V</B> macros.
  136. <p><h3>Working with AV's</h3>
  137. There are two ways to create and load an AV.  The first method just creates
  138. an empty AV:
  139. <p><pre>
  140.         AV*  newAV();
  141. </pre>
  142. The second method both creates the AV and initially populates it with SV's:
  143. <p><pre>
  144.         AV*  av_make(I32 num, SV **ptr);
  145. </pre>
  146. The second argument points to an array containing <B>num</B> <B>SV*</B>'s.
  147. <p>Once the AV has been created, the following operations are possible on AV's:
  148. <p><pre>
  149.         void  av_push(AV*, SV*);
  150.         SV*   av_pop(AV*);
  151.         SV*   av_shift(AV*);
  152.         void  av_unshift(AV*, I32 num);
  153. </pre>
  154. These should be familiar operations, with the exception of <B>av_unshift</B>.
  155. This routine adds <B>num</B> elements at the front of the array with the 
  156. <A HREF="perlfunc.html#perlfunc_258">undef</A>
  157.  
  158. value.  You must then use <B>av_store</B> (described below) to assign values
  159. to these new elements.
  160. <p>Here are some other functions:
  161. <p><pre>
  162.         I32   av_len(AV*); /* Returns length of array */
  163. </pre>
  164. <pre>
  165.         SV**  av_fetch(AV*, I32 key, I32 lval);
  166.                 /* Fetches value at key offset, but it seems to
  167.                set the value to lval if lval is non-zero */
  168.         SV**  av_store(AV*, I32 key, SV* val);
  169.                 /* Stores val at offset key */
  170. </pre>
  171. <pre>
  172.         void  av_clear(AV*);
  173.                 /* Clear out all elements, but leave the array */
  174.         void  av_undef(AV*);
  175.                 /* Undefines the array, removing all elements */
  176. </pre>
  177. If you know the name of an array variable, you can get a pointer to its AV
  178. by using the following:
  179. <p><pre>
  180.         AV*  perl_get_av("varname", FALSE);
  181. </pre>
  182. This returns NULL if the variable does not exist.
  183. <p><h3>Working with HV's</h3>
  184. To create an HV, you use the following routine:
  185. <p><pre>
  186.         HV*  newHV();
  187. </pre>
  188. Once the HV has been created, the following operations are possible on HV's:
  189. <p><pre>
  190.         SV**  hv_store(HV*, char* key, U32 klen, SV* val, U32 hash);
  191.         SV**  hv_fetch(HV*, char* key, U32 klen, I32 lval);
  192. </pre>
  193. The <B>klen</B> parameter is the length of the key being passed in.  The <B>val</B>
  194. argument contains the SV pointer to the scalar being stored, and <B>hash</B> is
  195. the pre-computed hash value (zero if you want <B>hv_store</B> to calculate it
  196. for you).  The <B>lval</B> parameter indicates whether this fetch is actually a
  197. part of a store operation.
  198. <p>Remember that <B>hv_store</B> and <B>hv_fetch</B> return <B>SV**</B>'s and not just
  199. <B>SV*</B>.  In order to access the scalar value, you must first dereference
  200. the return value.  However, you should check to make sure that the return
  201. value is not NULL before dereferencing it.
  202. <p>These two functions check if a hash table entry exists, and deletes it.
  203. <p><pre>
  204.         bool  hv_exists(HV*, char* key, U32 klen);
  205.         SV*   hv_delete(HV*, char* key, U32 klen);
  206. </pre>
  207. And more miscellaneous functions:
  208. <p><pre>
  209.         void   hv_clear(HV*);
  210.                 /* Clears all entries in hash table */
  211.         void   hv_undef(HV*);
  212.                 /* Undefines the hash table */
  213. </pre>
  214. <pre>
  215.         I32    hv_iterinit(HV*);
  216.                 /* Prepares starting point to traverse hash table */
  217.         HE*    hv_iternext(HV*);
  218.                 /* Get the next entry, and return a pointer to a
  219.                    structure that has both the key and value */
  220.         char*  hv_iterkey(HE* entry, I32* retlen);
  221.                 /* Get the key from an HE structure and also return
  222.                    the length of the key string */
  223.         SV*     hv_iterval(HV*, HE* entry);
  224.                 /* Return a SV pointer to the value of the HE
  225.                    structure */
  226. </pre>
  227. If you know the name of a hash variable, you can get a pointer to its HV
  228. by using the following:
  229. <p><pre>
  230.         HV*  perl_get_hv("varname", FALSE);
  231. </pre>
  232. This returns NULL if the variable does not exist.
  233. <p>The hash algorithm, for those who are interested, is:
  234. <p><pre>
  235.         i = klen;
  236.         hash = 0;
  237.         s = key;
  238.         while (i--)
  239.         hash = hash * 33 + *s++;
  240. </pre>
  241. <h3>References</h3>
  242. References are a special type of scalar that point to other scalar types
  243. (including references).  To treat an AV or HV as a scalar, it is simply
  244. a matter of casting an AV or HV to an SV.
  245. <p>To create a reference, use the following command:
  246. <p><pre>
  247.         SV*  newRV((SV*) pointer);
  248. </pre>
  249. Once you have a reference, you can use the following macro with a cast to
  250. the appropriate typedef (SV, AV, HV):
  251. <p><pre>
  252.         SvRV(SV*)
  253. </pre>
  254. then call the appropriate routines, casting the returned <B>SV*</B> to either an
  255. <B>AV*</B> or <B>HV*</B>.
  256. <p>To determine, after dereferencing a reference, if you still have a reference,
  257. you can use the following macro:
  258. <p><pre>
  259.         SvROK(SV*)
  260. </pre>
  261. <h2>XSUB'S and the Argument Stack</h2>
  262. The XSUB mechanism is a simple way for Perl programs to access C subroutines.
  263. An XSUB routine will have a stack that contains the arguments from the Perl
  264. program, and a way to map from the Perl data structures to a C equivalent.
  265. <p>The stack arguments are accessible through the <B>ST(n)</B> macro, which returns
  266. the <B>n</B>'th stack argument.  Argument 0 is the first argument passed in the
  267. Perl subroutine call.  These arguments are <B>SV*</B>, and can be used anywhere
  268. an <B>SV*</B> is used.
  269. <p>Most of the time, output from the C routine can be handled through use of
  270. the RETVAL and OUTPUT directives.  However, there are some cases where the
  271. argument stack is not already long enough to handle all the return values.
  272. An example is the POSIX tzname() call, which takes no arguments, but returns
  273. two, the local timezone's standard and summer time abbreviations.
  274. <p>To handle this situation, the PPCODE directive is used and the stack is
  275. extended using the macro:
  276. <p><pre>
  277.         EXTEND(sp, num);
  278. </pre>
  279. where <B>sp</B> is the stack pointer, and <B>num</B> is the number of elements the
  280. stack should be extended by.
  281. <p>Now that there is room on the stack, values can be pushed on it using the
  282. macros to push IV's, doubles, strings, and SV pointers respectively:
  283. <p><pre>
  284.         PUSHi(IV)
  285.         PUSHn(double)
  286.         PUSHp(char*, I32)
  287.         PUSHs(SV*)
  288. </pre>
  289. And now the Perl program calling <B>tzname</B>, the two values will be assigned
  290. as in:
  291. <p><pre>
  292.         ($standard_abbrev, $summer_abbrev) = POSIX::tzname;
  293. </pre>
  294. An alternate (and possibly simpler) method to pushing values on the stack is
  295. to use the macros:
  296. <p><pre>
  297.         XPUSHi(IV)
  298.         XPUSHn(double)
  299.         XPUSHp(char*, I32)
  300.         XPUSHs(SV*)
  301. </pre>
  302. These macros automatically adjust the stack for you, if needed.
  303. <p><h2>Mortality</h2>
  304. In Perl, values are normally "immortal" -- that is, they are not freed unless
  305. explicitly done so (via the Perl 
  306. <A HREF="perlfunc.html#perlfunc_258">undef</A>
  307.  call or other routines in Perl
  308. itself).
  309. <p>In the above example with <B>tzname</B>, we needed to create two new SV's to push
  310. onto the argument stack, that being the two strings.  However, we don't want
  311. these new SV's to stick around forever because they will eventually be
  312. copied into the SV's that hold the two scalar variables.
  313. <p>An SV (or AV or HV) that is "mortal" acts in all ways as a normal "immortal"
  314. SV, AV, or HV, but is only valid in the "current context".  When the Perl
  315. interpreter leaves the current context, the mortal SV, AV, or HV is
  316. automatically freed.  Generally the "current context" means a single
  317. Perl statement.
  318. <p>To create a mortal variable, use the functions:
  319. <p><pre>
  320.         SV*  sv_newmortal()
  321.         SV*  sv_2mortal(SV*)
  322.         SV*  sv_mortalcopy(SV*)
  323. </pre>
  324. The first call creates a mortal SV, the second converts an existing SV to
  325. a mortal SV, the third creates a mortal copy of an existing SV.
  326. <p>The mortal routines are not just for SV's -- AV's and HV's can be made mortal
  327. by passing their address (and casting them to <B>SV*</B>) to the <B>sv_2mortal</B> or
  328. <B>sv_mortalcopy</B> routines.
  329. <p><h2>Creating New Variables</h2>
  330. To create a new Perl variable, which can be accessed from your Perl script,
  331. use the following routines, depending on the variable type.
  332. <p><pre>
  333.         SV*  perl_get_sv("varname", TRUE);
  334.         AV*  perl_get_av("varname", TRUE);
  335.         HV*  perl_get_hv("varname", TRUE);
  336. </pre>
  337. Notice the use of TRUE as the second parameter.  The new variable can now
  338. be set, using the routines appropriate to the data type.
  339. <p><h2>Stashes and Objects</h2>
  340. A stash is a hash table (associative array) that contains all of the
  341. different objects that are contained within a package.  Each key of the
  342. hash table is a symbol name (shared by all the different types of
  343. objects that have the same name), and each value in the hash table is
  344. called a GV (for Glob Value).  The GV in turn contains references to
  345. the various objects of that name, including (but not limited to) the
  346. following:
  347.     
  348.     Scalar Value
  349.     Array Value
  350.     Hash Value
  351.     File Handle
  352.     Directory Handle
  353.     Format
  354.     Subroutine
  355. <p>Perl stores various stashes in a GV structure (for global variable) but
  356. represents them with an HV structure.
  357. <p>To get the HV pointer for a particular package, use the function:
  358. <p><pre>
  359.         HV*  gv_stashpv(char* name, I32 create)
  360.         HV*  gv_stashsv(SV*, I32 create)
  361. </pre>
  362. The first function takes a literal string, the second uses the string stored
  363. in the SV.
  364. <p>The name that <B>gv_stash*v</B> wants is the name of the package whose symbol table
  365. you want.  The default package is called <B>main</B>.  If you have multiply nested
  366. packages, it is legal to pass their names to <B>gv_stash*v</B>, separated by
  367. <B>::</B> as in the Perl language itself.
  368. <p>Alternately, if you have an SV that is a blessed reference, you can find
  369. out the stash pointer by using:
  370. <p><pre>
  371.         HV*  SvSTASH(SvRV(SV*));
  372. </pre>
  373. then use the following to get the package name itself:
  374. <p><pre>
  375.         char*  HvNAME(HV* stash);
  376. </pre>
  377. If you need to return a blessed value to your Perl script, you can use the
  378. following function:
  379. <p><pre>
  380.         SV*  sv_bless(SV*, HV* stash)
  381. </pre>
  382. where the first argument, an <B>SV*</B>, must be a reference, and the second
  383. argument is a stash.  The returned <B>SV*</B> can now be used in the same way
  384. as any other SV.
  385. <p><h2>Magic</h2>
  386. [This section under construction]
  387. <p><h2>Double-Typed SV's</h2>
  388. Scalar variables normally contain only one type of value, an integer,
  389. double, pointer, or reference.  Perl will automatically convert the
  390. actual scalar data from the stored type into the requested type.
  391. <p>Some scalar variables contain more than one type of scalar data.  For
  392. example, the variable 
  393. <A HREF="perlvar.html#perlvar_436">$!</A>
  394.  contains either the numeric value of <B>errno</B>
  395. or its string equivalent from <B>sys_errlist[]</B>.
  396. <p>To force multiple data values into an SV, you must do two things: use the
  397. <B>sv_set*v</B> routines to add the additional scalar type, then set a flag
  398. so that Perl will believe it contains more than one type of data.  The
  399. four macros to set the flags are:
  400. <p><pre>
  401.         SvIOK_on
  402.         SvNOK_on
  403.         SvPOK_on
  404.         SvROK_on
  405. </pre>
  406. The particular macro you must use depends on which <B>sv_set*v</B> routine
  407. you called first.  This is because every <B>sv_set*v</B> routine turns on
  408. only the bit for the particular type of data being set, and turns off
  409. all the rest.
  410. <p>For example, to create a new Perl variable called "dberror" that contains
  411. both the numeric and descriptive string error values, you could use the
  412. following code:
  413. <p><pre>
  414.         extern int  dberror;
  415.         extern char *dberror_list;
  416. </pre>
  417. <pre>
  418.         SV* sv = perl_get_sv("dberror", TRUE);
  419.         sv_setiv(sv, (IV) dberror);
  420.         sv_setpv(sv, dberror_list[dberror]);
  421.         SvIOK_on(sv);
  422. </pre>
  423. If the order of <B>sv_setiv</B> and <B>sv_setpv</B> had been reversed, then the
  424. macro <B>SvPOK_on</B> would need to be called instead of <B>SvIOK_on</B>.
  425. <p><h2>Calling Perl Routines from within C Programs</h2>
  426. There are four routines that can be used to call a Perl subroutine from
  427. within a C program.  These four are:
  428. <p><pre>
  429.         I32  perl_call_sv(SV*, I32);
  430.         I32  perl_call_pv(char*, I32);
  431.         I32  perl_call_method(char*, I32);
  432.         I32  perl_call_argv(char*, I32, register char**);
  433. </pre>
  434. The routine most often used should be <B>perl_call_sv</B>.  The <B>SV*</B> argument
  435. contains either the name of the Perl subroutine to be called, or a reference
  436. to the subroutine.  The second argument tells the appropriate routine what,
  437. if any, variables are being returned by the Perl subroutine.
  438. <p>All four routines return the number of arguments that the subroutine returned
  439. on the Perl stack.
  440. <p>When using these four routines, the programmer must manipulate the Perl stack.
  441. These include the following macros and functions:
  442. <p><pre>
  443.         dSP
  444.         PUSHMARK()
  445.         PUTBACK
  446.         SPAGAIN
  447.         ENTER
  448.         SAVETMPS
  449.         FREETMPS
  450.         LEAVE
  451.         XPUSH*()
  452. </pre>
  453. For more information, consult 
  454. <A HREF="perlcall.html">
  455. the perlcall manpage</A>
  456. .
  457. <p><h2>Memory Allocation</h2>
  458. [This section under construction]
  459. <p><h2>AUTHOR</h2>
  460. Jeff Okamoto <okamoto@corp.hp.com>
  461. <p>With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
  462. Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, and Neil
  463. Bowers.
  464. <p><h2>DATE</h2>
  465. Version 12: 1994/10/16
  466. <p>